<template>
  <div class="box"></div>
  <div>
    <input type="text" v-model.number="num" />
    <button @click="search">操作</button>
  </div>
  <div>
    {{ $t('hello') }}
    <select v-model="$i18n.locale">
      <option v-for="locale in $i18n.availableLocales" :key="`locale-${locale}`" :value="locale">{{ locale }}</option>
    </select>
    <button @click="changeLang">改变</button>
  </div>
</template>
<script>
import i18n from '@/locales/index'
import data from './json/s.js'
import dataFix from './json/s_fix.js'
export default {
  data() {
    return {
      num: 15
    }
  },
  methods: {
    search() {
      let sellnum = data
        .filter((res) => {
          return res.p < this.num && res.e != 1
        })
        .reduce((memo, res) => {
          return memo + (res.a == 0 ? res.c : -res.c)
        }, 0)
      console.log(`snum`, sellnum)
    },
    calSome(data, name) {
      console.log(name, data)
      let all_buy_cost = 0
      let all_buy_num = 0
      let allnum = data.reduce((memo, res) => {
        if (res.a == 0) {
          memo = memo + res.c
          all_buy_num += res.c
        } else {
          console.log(`S_num`, res.c)
          memo = memo - res.c
        }
        return memo
      }, 0)
      console.log(allnum)

      let allcost = 0
      data.forEach((res) => {
        if (res.a == 0) {
          allcost = allcost + res.p * res.c
          all_buy_cost += res.p * res.c
        } else {
          console.log(`S_price`, res.p * res.c)
          allcost = allcost - res.p * res.c
        }
      })

      console.log(allcost, allcost / allnum.toFixed(2), all_buy_cost, all_buy_num, all_buy_cost / all_buy_num.toFixed(2))

      let i = data.length - 1
      while (i > 0) {
        data[i].diff = ((data[i].p / data[i - 1].p - 1) * 100).toFixed(2) + '%'
        i = i - 1
      }
    },
    changeLang(lang) {
      i18n.global.locale = 'zh'
    }
  },
  mounted() {
    this.calSome(data, 'data')
    this.calSome(dataFix, 'dataFix')
  }
}
</script>
<style type="text/css">
.box {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
}
</style>
